package com.example.demo6tcp3;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ServeReader extends Thread {

    private Socket socket;

    public ServeReader(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            // 读取管道信息
            // 3.获取输入流，读取客户端发送的数据
            InputStream is = socket.getInputStream();

            // 4.把字节输入流包装成特殊数据输入流
            DataInputStream dis = new DataInputStream(is);

            // 5.读取数据
            while (true) {
                String msg = dis.readUTF();
                System.out.println("收到的客户端msg=" + msg);

                // 6.获取客户端的ip和端口
                System.out.println("客户端的ip=" + socket.getInetAddress().getHostAddress());
                System.out.println("客户端的端口=" + socket.getPort());
                System.out.println("-----------------------------------");
            }
        } catch (IOException e) {
            System.out.println("客户端下线了" + socket.getInetAddress().getHostAddress());
        }
    }
}
